package com.principalservice.controller;

import annotation.DisableBaseResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.principalservice.service.DeptService;
import com.principalservice.service.UserInfoService;
import generator.domain.Dept;
import generator.domain.UserInfo;
import generator.support.ResponseResult;
import generator.vo.addEmp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.jws.soap.SOAPBinding;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 员工管理Controller
 */
@RestController
@RequestMapping("/emp")
@CrossOrigin
@Slf4j
public class EmpController {

    @Autowired
    UserInfoService userInfoService;

    @Autowired
    DeptService deptService;

    /**
     * 分页查询员工信息，一页10条数据
     * @return 员工信息
     */
    @GetMapping("/getemp")
    public List<Map<String, Object>> getUserInfoByPage(){
        log.info("EmpController -> getUserInfoByPage");
        List<Map<String, Object>> result = new ArrayList<>();
        List<UserInfo> list = userInfoService.list();
        for (UserInfo userInfo : list) {
            Map<String, Object> map = new HashMap<>();
            map.put("User", userInfo);
            QueryWrapper<Dept> deptQueryWrapper = new QueryWrapper<>();
            deptQueryWrapper.eq("dept_id", userInfo.getUserDeptId());
            List<Dept> deptList = deptService.list(deptQueryWrapper);
            if (!deptList.isEmpty()){
                map.put("Dept", deptList.get(0));
            }
            result.add(map);
        }
        return result;
    }

    /**
     * 员工信息模糊查询
     * @param userName 员工姓名
     * @param dept 员工部门
     * @return 员工信息
     */
    @GetMapping("/queryemp")
    public List<Map<String, Object>> getUserInfoByNameOrDept(@RequestParam(required = false) String userName,
                                                             @RequestParam(required = false) String dept){
        log.info("EmpController -> getUserInfoByNameOrDept");
        QueryWrapper<UserInfo> qw = new QueryWrapper<>();
        // 根据名称获取部门id
        if (dept != null){
            QueryWrapper<Dept> deptQueryWrapper = new QueryWrapper<>();
            deptQueryWrapper.eq("dept_name", dept);
            List<Dept> list = deptService.list(deptQueryWrapper);
            qw.eq("user_dept_id", list.get(0).getDeptId());
        }
        if (userName != null){
            qw.like("user_name", userName);
        }
        List<Map<String, Object>> result = new ArrayList<>();
        List<UserInfo> list = userInfoService.list(qw);
        for (UserInfo userInfo : list) {
            Map<String, Object> map = new HashMap<>();
            map.put("User", userInfo);
            QueryWrapper<Dept> deptQueryWrapper = new QueryWrapper<>();
            deptQueryWrapper.eq("dept_id", userInfo.getUserDeptId());
            List<Dept> deptList = deptService.list(deptQueryWrapper);
            if (!deptList.isEmpty()){
                map.put("Dept", deptList.get(0));
            }
            result.add(map);
        }
        return result;
    }

    /**
     * 添加员工
     * @param newEmp 新员工信息
     * @return 是否添加成功
     */
    @PostMapping("/addEmp")
    @DisableBaseResponse
    public ResponseResult<Object> addEmp(@RequestBody addEmp newEmp){
        // 查看员工编号是否存在
        QueryWrapper<UserInfo> userQw = new QueryWrapper<>();
        userQw.eq("user_info_no", newEmp.getUserInfoNo());
        List<UserInfo> list1 = userInfoService.list(userQw);
        if (!list1.isEmpty()){
            return new ResponseResult<>(400, "该员工编号已存在");
        }

        // 根据部门名称查询部门id
        QueryWrapper<Dept> deptqw = new QueryWrapper<>();
        deptqw.eq("dept_name", newEmp.getDept());
        List<Dept> list = deptService.list(deptqw);
        if (list.isEmpty()){
            return new ResponseResult<>(400, "该部门不存在，请重新选择");
        }
        Integer deptId = list.get(0).getDeptId();

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:dd:ss");
        UserInfo newUser = new UserInfo();
        newUser.setUserInfoNo(newEmp.getUserInfoNo());
        newUser.setUserName(newEmp.getUserName());
        newUser.setUserDeptId(deptId);
        newUser.setUserInfoSex(newEmp.getUserInfoSex());
        newUser.setUserInfoPhone(newEmp.getUserInfoPhone());
        newUser.setUserInfoEntryTime(newEmp.getUserInfoEntryTime());
        newUser.setUserInfoCreateTime(format.format(new Date()));
        if (userInfoService.save(newUser)){
            return new ResponseResult<>();
        }
        return new ResponseResult<>(400, "添加失败，请检查信息");
    }

    /**
     * 根据ID删除员工信息
     * @param id 员工ID
     * @return 是否删除成功
     */
    @GetMapping("/deleteEmp/{id}")
    @DisableBaseResponse
    public ResponseResult<Object> deleteEmp(@PathVariable String id){
        log.info("EmpController -> deleteEmp");
        System.out.println(id);
        if (userInfoService.removeById(Integer.parseInt(id))){
            return new ResponseResult<>();
        }
        return new ResponseResult<>(400, "删除失败，请刷新页面");
    }

    /**
     * 修改员工信息
     * @param emp 员工信息
     * @return 是否修改成功
     */
    @PostMapping("/editEmp")
    @DisableBaseResponse
    public ResponseResult<Object> editEmp(@RequestBody addEmp emp){
        // 查看员工编号是否存在
        QueryWrapper<UserInfo> userQw = new QueryWrapper<>();
        userQw.eq("user_info_no", emp.getUserInfoNo());
        List<UserInfo> list1 = userInfoService.list(userQw);
        if (!list1.isEmpty() && !list1.get(0).getUserInfoId().equals(emp.getUserInfoId())){
            return new ResponseResult<>(400, "该员工编号已存在");
        }

        // 根据部门名称查询部门id
        QueryWrapper<Dept> deptqw = new QueryWrapper<>();
        deptqw.eq("dept_name", emp.getDept());
        List<Dept> list = deptService.list(deptqw);
        if (list.isEmpty()){
            return new ResponseResult<>(400, "该部门不存在，请重新选择");
        }
        Integer deptId = list.get(0).getDeptId();

        UpdateWrapper<UserInfo> qw = new UpdateWrapper<>();
        qw.eq("user_info_id", emp.getUserInfoId());
        qw.set("user_info_no", emp.getUserInfoNo());
        qw.set("user_name", emp.getUserName());
        qw.set("user_info_sex", emp.getUserInfoSex());
        qw.set("user_dept_id", deptId);
        if (userInfoService.update(qw)){
            return new ResponseResult<>();
        }
        return new ResponseResult<>(400, "更新失败，请检查信息是否正确");
    }
}
